package com.magic.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.himalaya.R;
import com.magic.himalaya.utils.Constants;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/1/22.
 */
public class TrackListAdapter extends RecyclerView.Adapter<TrackListAdapter.ItemViewHolder> {
    private static final String TAG = TrackListAdapter.class.getSimpleName();
    private List<Track> mTrackList = new ArrayList<>();
    private OnClickCallBack onClickCallBack = null;

    /**
     * 渲染每一个ITEM数据
     *
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tracks_recyclerview, parent, false);
        return new ItemViewHolder(view);
    }

    /**
     * 绑定数据，绑定每一个Item的数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.itemView.setTag(position);//缓存
        View itemView = holder.itemView;

        //////////获取组件并渲染数据
        Track track = mTrackList.get(position);
        TextView track_idx = itemView.findViewById(R.id.track_idx);
        TextView track_title = itemView.findViewById(R.id.track_title);
        TextView track_play_count = itemView.findViewById(R.id.track_play_count);
        TextView track_play_download = itemView.findViewById(R.id.track_play_duration);
        TextView track_date = itemView.findViewById(R.id.track_date);
        track_idx.setText((position + 1) + "");
        track_title.setText(track.getTrackTitle() + "");
        track_play_count.setText(track.getPlayCount() + "");
        //由于simpleFormat转换是 为毫秒为基准，需要将时间*1000
        track_play_download.setText(" " + Constants.timeFormat.format(track.getDuration() * 1000));
        track_date.setText(Constants.dateFormat.format(new Date(track.getCreatedAt())));

        //设置点击事件
        if (onClickCallBack != null)
            //将点击的位置坐标传送过去，再带一个声音列表(用于加入到播放器中)
            itemView.setOnClickListener(v -> onClickCallBack.setCallback(mTrackList, position));

    }

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        this.onClickCallBack = onClickCallBack;
    }

    public interface OnClickCallBack {
        /**
         * 回调一个声音列表(List<Track>)回去和postion(当前点击位置)
         *
         * @param tracks   声音列表集合
         * @param position 当前播放的位置
         */
        void setCallback(List<Track> tracks, int position);
    }

    /**
     * 设置适配器中的数据，设置后再进行渲染
     *
     * @param trackList
     */
    public void setData(List<Track> trackList) {
        if (trackList != null) {
            this.mTrackList.clear();
            this.mTrackList.addAll(trackList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mTrackList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
